import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/utils/color_util.dart';

class InputItem extends StatelessWidget {

  final String title;
  final String defaultValue;
  final String hintText;
  final bool showBorder;
  final ValueChanged<String> onChenge;
  final TextInputType keyBoardType;
  final bool obscureText;
  final bool showErrorText;
  final String errorText;
  final int? maxLength;
  final List<TextInputFormatter>? inputFormatters;
  final bool readOnly;

  InputItem({
    required this.title,
    required this.hintText,
    required this.onChenge,
    this.defaultValue = '',
    this.showBorder = true,
    this.obscureText = false,
    this.keyBoardType = TextInputType.text,
    this.showErrorText = false,
    this.errorText = '',
    this.maxLength,
    this.inputFormatters,
    this.readOnly = false
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 4.w),
        height: 54.h,
        decoration: BoxDecoration(
          border: Border(
            bottom: showBorder ? BorderSide(
              width: .5,
              color: lineColor
            ) : BorderSide.none
          )
        ),
        child: Row(
          children: [
            Container(
              width: 84.w,
              child: Text(
                title,
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 16.sp
                ),
              ),
            ),
            Expanded(
              child: TextField(
                // controller: TextEditingController(text: defaultValue),
                controller: TextEditingController.fromValue(TextEditingValue(
                  // 输入的文本
                  text: defaultValue,
                  // 保持光标在最后
                  selection: TextSelection.fromPosition(
                    TextPosition(
                      affinity: TextAffinity.downstream,
                      offset: defaultValue.length
                    )
                  ))
                ),
                readOnly: readOnly,
                inputFormatters: inputFormatters,
                decoration: InputDecoration(
                  border: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                  enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                  focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
                  contentPadding: EdgeInsets.zero,
                  counterText: '',
                  hintText: hintText,
                  hintStyle: TextStyle(
                    fontSize: 16.sp,
                    color: textSubtitleColor,
                    fontWeight: FontWeight.normal
                  )
                ),
                cursorColor: textBlackColor,
                obscureText: obscureText,
                keyboardType: keyBoardType,
                maxLength: maxLength,
                style: TextStyle(
                  fontSize: 16.sp,
                  color: textBlackColor,
                  fontWeight: FontWeight.normal
                ),
                onChanged: (String value) => onChenge(value),
              ),
            ),
            showErrorText ? Text(
              errorText,
              style: TextStyle(
                fontSize: 12.sp,
                color: mainTextColor
              ),
            ) : Container()
          ],
        ),
      ),
    );
  }
}